﻿Imports UtilPadrao
Imports Controle
Public Class frmGrupoAcesso

#Region "Variáveis"

    Private objGrupoAcesso As New Controle.ctrGrupoAcesso
    Private objModulos As New Controle.ctrModulos

    Private datMenus As New DataTable

    Private iCodModuOriginal As Integer

#End Region

#Region "Eventos Formulário"

    Private Sub frmGrupoAcesso_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.listarModulos()
    End Sub

    Private Sub frmGrupoAcesso_alteraModo(ByVal eModoAtual As FronteiraPadrao.frmPadrao.eModo) Handles Me.alteraModo

        If (eModoAtual = eModo.eAguardando) Then
            Me.grpPrincipal.Enabled = False
            Me.grpGrid.Enabled = False
        Else
            Me.grpPrincipal.Enabled = True
            Me.grpGrid.Enabled = True
        End If

        Me.txtCodigo.Enabled = (eModoAtual = eModo.eAguardando)
        Me.btnCodigo.Enabled = (eModoAtual = eModo.eAguardando)

    End Sub

    Private Sub frmGrupoAcesso_excluir(ByRef bCancel As Boolean) Handles Me.excluir

        Try

            If Me.objGrupoAcesso.excluirGrupoAcesso(conversao.toInt32(Me.txtCodigo.Text)) Then
                bCancel = False
            End If

        Catch ex As Exception
            MessageBox.Show("Exclusão cancelada. Existem registros vinculados.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub

    Private Sub frmGrupoAcesso_inserir(ByRef bCancel As Boolean) Handles Me.inserir
        Me.cboModulo.Enabled = True
    End Sub

    Private Sub frmGrupoAcesso_gravar(ByRef bCancel As Boolean) Handles Me.gravar

        Try

            bCancel = True

            If Me.objGrupoAcesso.gravarGrupoAcesso(conversao.toInt32(Me.txtCodigo.Text),
                                                   Me.txtNome.Text,
                                                   Me.cboModulo.SelectedValue,
                                                   Me.iCodModuOriginal,
                                                   Me.datMenus) Then

                MyBase.chave = Me.objGrupoAcesso.codigoGrupo

                MessageBox.Show("Grupo de Acesso salvo com sucesso.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                bCancel = False

            Else
                Me.Mensagens = Me.objGrupoAcesso.retornaMensagensValidacao
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub frmGrupoAcesso_carregaDados(ByVal iChave As Integer) Handles Me.carregaDados

        Try

            If iChave > 0 Then

                Dim drGrupoAcesso As DataRow

                drGrupoAcesso = Me.objGrupoAcesso.selecionarGrupoAcessoCodigo(iChave, 0, 0)

                If Not drGrupoAcesso Is Nothing Then

                    Me.txtCodigo.Text = iChave
                    Me.txtNome.Text = drGrupoAcesso.Item("nome")
                    Me.cboModulo.SelectedValue = drGrupoAcesso.Item("codModulo")
                    Me.iCodModuOriginal = drGrupoAcesso.Item("codModulo")

                    Me.datMenus = Me.objGrupoAcesso.selecionarGrupoAcessoMenus(iChave,
                                                                               drGrupoAcesso.Item("codModulo"))

                    Me.dgvGrid.DataSource = Me.datMenus

                    Me.cboModulo.Enabled = Not Me.objGrupoAcesso.seGrupoUsuarios(iChave)

                Else
                    MessageBox.Show("Identificador inválido.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    MyBase.chave = 0
                End If

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub frmGrupoAcesso_limpaCampo() Handles Me.limpaCampo
        Me.limpar()
    End Sub

    Private Sub frmGrupoAcesso_cancelar(ByRef bCancel As Boolean) Handles Me.cancelar
        Me.epPadrao.Clear()
    End Sub

    Private Sub frmGrupoAcesso_limpaProvedorDeErros() Handles Me.limpaProvedorDeErros
        Me.epPadrao.Clear()
    End Sub

    Private Sub frmGrupoAcesso_setarProvedorDeErros() Handles Me.setarProvedorDeErros

        Dim iContadorMensagens As Int32
        Dim sMensagem As String

        For iContadorMensagens = 0 To MyBase.Mensagens(FronteiraPadrao.frmPadrao.eMensagem.Codigo).Count - 1

            sMensagem = MyBase.Mensagens(FronteiraPadrao.frmPadrao.eMensagem.Mensagem).item(iContadorMensagens)

            Select Case MyBase.Mensagens(FronteiraPadrao.frmPadrao.eMensagem.Codigo).item(iContadorMensagens)
                Case ctrGrupoAcesso.eMensagens.MODULO, ctrGrupoAcesso.eMensagens.MODULO
                    Me.epPadrao.SetError(Me.cboModulo, sMensagem)
                Case ctrGrupoAcesso.eMensagens.GRUPO, ctrGrupoAcesso.eMensagens.MODULO
                    Me.epPadrao.SetError(Me.txtNome, sMensagem)
            End Select

        Next

    End Sub

#End Region

#Region "Eventos"

    Private Sub btnCodigo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCodigo.Click

        Dim datGrupoAcesso As New DataTable
        Dim objAuxiliar As Object()

        Try

            datGrupoAcesso = Me.objGrupoAcesso.selecionarGrupoAcesso

            If datGrupoAcesso.Rows.Count > 0 Then

                objAuxiliar = FronteiraPadrao.frmPesquisa.executarPesquisaGenerica("Selecionar Grupo de Acesso",
                                                                                    Me.objGrupoAcesso.sCamposTabela,
                                                                                    Me.objGrupoAcesso.sCamposDemonstracao,
                                                                                    Me.objGrupoAcesso.sCamposChave,
                                                                                    Me.objGrupoAcesso.sTitulosColunas,
                                                                                    datGrupoAcesso)
                If Not Verificador.seVazio(objAuxiliar) Then
                    Me.txtNome.Text = objAuxiliar(1)
                    MyBase.chave = objAuxiliar(0)
                End If

            Else
                MessageBox.Show("Não existem grupos de acesso cadastrados para a pesquisa.",
                                Me.Text,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub txtCodigo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCodigo.LostFocus

        Try

            If conversao.toInt32(Me.txtCodigo.Text) > 0 Then
                MyBase.chave = Me.txtCodigo.Text
            Else
                Me.limpar()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub cboModulo_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboModulo.DropDownClosed

        If Me.iCodModuOriginal <> Me.cboModulo.SelectedValue Then

            Me.datMenus = Me.objGrupoAcesso.selecionarGrupoAcessoMenus(conversao.toInt32(Me.txtCodigo.Text),
                                                           Me.cboModulo.SelectedValue)

            Me.dgvGrid.DataSource = Me.datMenus

        End If

    End Sub

    Private Sub dgvGrid_CellContentClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvGrid.CellContentClick

        Try

            If e.RowIndex >= 0 Then

                Me.dgvGrid.CommitEdit(DataGridViewDataErrorContexts.Commit)

                Select Case Me.dgvGrid.CurrentCell.OwningColumn.Name

                    Case "total"

                        Me.dgvGrid.CurrentRow.Cells("permissaoInserir").Value = conversao.toBoolean(Me.dgvGrid.CurrentRow.Cells("total").Value)
                        Me.dgvGrid.CurrentRow.Cells("permissaoExcluir").Value = conversao.toBoolean(Me.dgvGrid.CurrentRow.Cells("total").Value)
                        Me.dgvGrid.CurrentRow.Cells("permissaoAlterar").Value = conversao.toBoolean(Me.dgvGrid.CurrentRow.Cells("total").Value)
                        Me.dgvGrid.CurrentRow.Cells("permissaoConsultar").Value = conversao.toBoolean(Me.dgvGrid.CurrentRow.Cells("total").Value)

                    Case "permissaoInserir", "permissaoExcluir", "permissaoAlterar", "permissaoConsultar"

                        If Not conversao.toBoolean(Me.dgvGrid.CurrentRow.Cells(e.ColumnIndex).Value) Then
                            Me.dgvGrid.CurrentRow.Cells("total").Value = False
                        End If

                End Select

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub dgvGrid_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgvGrid.KeyDown

        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
        End If

    End Sub

    Private Sub btnPermissoes_Click(sender As System.Object, e As System.EventArgs) Handles btnPermissoes.Click

        Try

            If Me.cboModulo.SelectedValue > 0 Then

                Dim frmCopia As New frmCopiarPermissoes

                frmCopia.nomeModulo = Me.cboModulo.Text
                frmCopia.codigoModulo = Me.cboModulo.SelectedValue
                frmCopia.codigoSelecionado = conversao.toInt32(Me.txtCodigo.Text)
                frmCopia.ShowDialog()

                If frmCopia.datSelecao.Rows.Count > 0 Then
                    Me.datMenus = frmCopia.datSelecao.Copy
                    Me.dgvGrid.DataSource = Me.datMenus
                End If
            Else
                MessageBox.Show("Selecione o Módulo.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub chkMarcarTodos_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkMarcarTodos.CheckedChanged

        If Me.chkMarcarTodos.Checked Then
            Me.alterarEstado(True)
        Else
            Me.alterarEstado(False)
        End If

    End Sub

#End Region

#Region "Métodos"

    Private Sub listarModulos()

        Dim datModulos As New DataTable

        Try

            datModulos = Me.objModulos.listarModulos

            If datModulos.Rows.Count > 0 Then
                Me.cboModulo.DataSource = datModulos
                Me.cboModulo.DisplayMember = "descricao"
                Me.cboModulo.ValueMember = "codModulo"
                Me.cboModulo.SelectedValue = datModulos.Rows(0).Item("codModulo")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub limpar()
        MyBase.limpaCampos(Me)
        Me.datMenus.Rows.Clear()
        Me.iCodModuOriginal = 0
    End Sub

    Private Sub alterarEstado(ByVal bEstado As Boolean)

        For Each drLinha As DataGridViewRow In Me.dgvGrid.Rows
            Me.dgvGrid.Item("total", drLinha.Index).Value = bEstado
            Me.dgvGrid.Item("permissaoInserir", drLinha.Index).Value = bEstado
            Me.dgvGrid.Item("permissaoExcluir", drLinha.Index).Value = bEstado
            Me.dgvGrid.Item("permissaoAlterar", drLinha.Index).Value = bEstado
            Me.dgvGrid.Item("permissaoConsultar", drLinha.Index).Value = bEstado
        Next

    End Sub

#End Region



End Class